package be;

import java.util.ArrayList;

public class InsertList {
	
	private class Var{
		String type;
		String columnName;
		String value;
		
		public Var(String var,String name, String value){
			this.type = var;
			this.columnName = name;
			this.value = value;
		}
		
		public String getValue(){
			if(type.equals("varchar") || type.equals("date")){
				return "'" + value + "'";
			}
			return value;
		}
		public String getType(){
			return type;
		}
		public String getColumnName(){
			return columnName;
		}
	}
	
	public ArrayList<Var> table;
	
	
	public String getVars(){
		StringBuilder culumns = new StringBuilder();
		StringBuilder values = new StringBuilder();
		
		culumns.append("(");
		values.append("VALUES (");

		for(Var tmp : table){
			if(tmp.getColumnName() != "") culumns.append(tmp.getColumnName()+",");
			if(tmp.getValue() != "") values.append(tmp.getValue()+",");
		}
		
		culumns = culumns.deleteCharAt(culumns.length()-1);
		values = values.deleteCharAt(values.length()-1);	
		
		culumns.append(")");
		values.append(")");
		
		
		return culumns.append(values).toString();
	}
	
	/***
	 * Returns 'ColumnName' = 'VALUE', 'ColumnName' = 'VALUE'.......
	 * @return
	 */
	public String getUpdateVars(){
		StringBuilder sb = new StringBuilder();
		
		for(Var tmp : table){
			sb.append(tmp.getColumnName()+"=");
			sb.append(tmp.getValue()+",");
		}
		
		sb = sb.deleteCharAt(sb.length()-1);
		
		return sb.toString();
	}
	
	/***
	 * Returns 'ColumnName' = 'VALUE' AND 'ColumnName' = 'VALUE'.......
	 * @return
	 */
	public String getWhereVars(){
		StringBuilder sb = new StringBuilder();
		
		for(Var tmp : table){
			sb.append(tmp.getColumnName()+"=");
			sb.append(tmp.getValue()+" AND ");
		}
		
		sb = sb.delete(sb.length()-5, sb.length());
		
		return sb.toString();
	}
	
	
	/***
	 * Returns 'ColumnName', 'ColumnName', ...
	 * @return
	 */
	public String getSelectVars(){
		StringBuilder sb = new StringBuilder();
		
		for(Var tmp : table){
			sb.append(tmp.getColumnName()+",");
		}
		
		sb = sb.deleteCharAt(sb.length()-1);
		
		return sb.toString();
	}
	
	
	public InsertList(){
		table = new ArrayList<Var>();
	}
	
	public InsertList(String columnName){
		table = new ArrayList<Var>();
		addVariable(columnName, "varchar", columnName);
	}
	
	public InsertList(String columnName, String type, String value){
		table = new ArrayList<Var>();
		addVariable(columnName, type, value);
	}

	public void addInt(String columnName, String value){
		table.add(new Var("int",columnName,value));
	}
	
	public void addDate(String columnName, String value){
		table.add(new Var("date",columnName,value));
	}
	
	public void addVarchar(String columnName, String value){
		table.add(new Var("varchar",columnName, value));
	}
	
	public void addFloat(String columnName, String value){
		table.add(new Var("float",columnName,value));
	}

	public void addVariable(String columnName, String type, String value) {
		if(columnName.equals("") || type.equals("")){
			System.out.println("INSERTLIST addVariable: WARNING: EMPTY VARS!");
		}
		if(type.toLowerCase().trim().equals("int")){
			addInt(columnName, value);
		}
		else if(type.toLowerCase().trim().equals("varchar")){
			addVarchar(columnName, value);
		}
		else if(type.toLowerCase().trim().equals("float")){
			addFloat(columnName, value);
		}else if(type.toLowerCase().trim().equals("date")){
			addDate(columnName, value);
		}
	}
	public String spEndTag(){
		String ret = "";
		
		for(Var tmp : table){
			if(tmp.type.equals("int")){
				ret +="i";
				continue;
			}
			if(tmp.type.equals("date")){
				ret +="d";
				continue;
			}
			if(tmp.type.equals("varchar")){
				ret +="v";
				continue;
			}
			if(tmp.type.equals("float")){
				ret +="f";
				continue;
			}
		}
		return ret;
	}
	
	public String generateSPParam(){
		StringBuilder s = new StringBuilder();
		int i =0;
		for(Var tmp : table){
			if(tmp.getColumnName() != ""){
				s.append("IN ");s.append(i+"param ");s.append(tmp.getType());
				if(tmp.getType().equals("varchar")){
					s.append("(100)");
				}
				s.append(",");
			}
			i++;
		}
		s.deleteCharAt(s.length()-1);
		return s.toString();
	}
	
	public String selSPParam(){
		StringBuilder s = new StringBuilder();
		int i =0;
		for(Var tmp : table){
			if(tmp.getColumnName() != ""){
				s.append("IN ");s.append(i+"sparam ");s.append(tmp.getType());
				if(tmp.getType().equals("varchar")){
					s.append("(100)");
				}
				s.append(",");
			}
			i++;
		}
		s.deleteCharAt(s.length()-1);
		return s.toString();
	}
	public String spInsert(){
		StringBuilder culumns = new StringBuilder();
		StringBuilder values = new StringBuilder();
		
		culumns.append("(");
		values.append("VALUES (");
		int i =0;
		for(Var tmp : table){
			if(tmp.getColumnName() != "") culumns.append(tmp.getColumnName()+",");
			values.append(i+"param,");
			i++;
		}
		
		culumns = culumns.deleteCharAt(culumns.length()-1);
		values = values.deleteCharAt(values.length()-1);	
		
		culumns.append(")");
		values.append(");");
		
		
		return culumns.append(values).toString();
	}
	public String spValues(){
		StringBuilder values = new StringBuilder();
		for(Var tmp : table){
			if (tmp.type.equals("varchar")){
				values.append("'");
			}
			values.append(tmp.value);
			if (tmp.type.equals("varchar")){
				values.append("'");
			}
			values.append(",");
			
		}
		values = values.deleteCharAt(values.length()-1);	
		return values.toString();
		
		
	}
	public String spUpdate(){
		StringBuilder ret = new StringBuilder();
		int i = 0;
		for(Var tmp:table){
			ret.append(tmp.columnName);
			ret.append("=");
			ret.append(i);
			ret.append("param,");
			i++;
		}
		ret.deleteCharAt(ret.length()-1);
		return ret.toString();
	}
	public String spSelect(){
		StringBuilder select = new StringBuilder();
		int i =0;
		for(Var tmp:table){
			if(tmp.getColumnName()=="")continue;
			select.append(tmp.columnName);
			select.append("=");
			select.append(i);
			select.append("sparam,");
			i++;
		}
		select.deleteCharAt(select.length()-1);
		return select.toString();
	}
	public String spCompare(){
		StringBuilder comp = new StringBuilder();
		int i = 0;
		for(Var tmp:table){
			comp.append(tmp.columnName);
			comp.append("=");
			comp.append(i);
			comp.append("param,");
			i++;
		}
		comp.deleteCharAt(comp.length()-1);
		return comp.toString();
	}
	public String spSelectCall(){
		StringBuilder values = new StringBuilder();
		for(Var tmp : table){
			values.append(tmp.columnName);
			values.append(",");
			
		}
		values = values.deleteCharAt(values.length()-1);	
		return values.toString();
	}
}
